//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {

    // 产品列表
    items: [],

    // 数据加载
    loading: false,

    // 加载提示
    loadTips: '',

    // 星星数
    star: [0, 1, 2, 3, 4],

    // 星星评分
    starNum: [0, 0, 0, 0],

    // 选择的图片
    selectImg: [],

    //最终上传的图片
    imgs: [],

  },

  // 页面加载
  onLoad: function (options) {
    this.setData({
      options: options
    });
  },

  // 选择星星
  selectStar: function (event) {
    var that = this;
    var type = event.currentTarget.dataset.type;
    var index = event.currentTarget.dataset.index + 1;
    var num = 0;
    if (type == 0) {
      that.setData({
        'starNum[0]': index,
        cost: index
      })
    } else if (type == 1) {
      that.setData({
        'starNum[1]': false,
        'starNum[2]': false
      })
      if (event.currentTarget.dataset.status=="zan"){
        that.setData({
          'starNum[1]': true,
          privatepm:1
        })
      }else{
        that.setData({
          'starNum[2]': true,
          privatepm:0
        })
      }
    } else if (type == 2) {
      that.setData({
        'starNum[3]': false,
        'starNum[4]': false
      })
      if (event.currentTarget.dataset.status == "zan") {
        that.setData({
          'starNum[3]': true,
          speed: 1
        })
      } else {
        that.setData({
          'starNum[4]': true,
          speed: 0
        })
      }
    } else if (type == 3) {
      that.setData({
        'starNum[5]': false,
        'starNum[6]': false
      })
      if (event.currentTarget.dataset.status == "zan") {
        that.setData({
          'starNum[5]': true,
          service: 1
        })
      } else {
        that.setData({
          'starNum[6]': true,
          service: 0
        })
      }
    }
    console.log(that.data.starNum);

  },

  // 用户输入
  getTextarea: function (event) {
    var txt = event.detail.value
    console.log(txt);
    this.setData({
      textarea: txt
    })
  },

  // 验证
  verify: function (fn) {
    var that = this;
    if (!that.data.textarea) {
      that.tips('请输入评价内容');
      return false;
    } else if (that.data.starNum[0] == 0) {
      that.tips('请给“描述相符”评星');
      return false;
    }  else if (that.data.privatepm == undefined) {
      that.tips('请给“菜品质量”评星');
      return false;
    } else if (that.data.speed == undefined) {
      that.tips('请给“出品速度”评星');
      return false;
    } else if (that.data.service == undefined) {
      that.tips('请给“服务质量”打分');
      return false;
    }
    return true;
  },
  // 最终提交（确定发表）
  submit: function () {
    var that = this;

    if (that.verify()) {
      // that.submitData();
      if (that.data.selectImg.length > 0) { // 有图片先上传图片
        for (var i = 0; i < that.data.selectImg.length; i++) {

          if (i == that.data.selectImg.length - 1) {
            console.log(i+"aaaaa");
            that.uploadImg(that.data.selectImg[i], function () {
              that.submitData();
            })
          } else {
            that.uploadImg(that.data.selectImg[i], function () {
            })
            console.log(that.data.img);
          }
        }
      } else { // 没图片
        that.submitData();
      }
    }
  },
  aaa: function () {

  },
  // 提交评论信息
  submitData: function () {
    var that = this;
    app.getLoginSession(function (session) {
      console.log(that.data.options);
      wx.request({
        url: app.api +"?app=evaluation_buyer&act=add_comment",
        data: {
          // app: 'evaluation_buyer',
          // act: 'add_comment',
          store_id: that.data.options.store_id,
          order_id:that.data.options.order_id,
          buyer_comment: that.data.textarea,
          img: that.data.imgs,
          cost: that.data.starNum[0],
          privatepm:that.data.privatepm,
          speed:that.data.speed,
          service:that.data.service,
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log("成功");
          var data = res.data;
          console.log(data);

          if (data.done) {
            wx.showModal({
              title: '提示',
              content: data.retval,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack();
                } else {
                  wx.navigateBack();
                }

              }
            })
          } else {
            wx.showToast({
              title: '提交失败'
            })
          }
        }, fail: function () {
          that.setData({
            loading: true,
            errorTips: app.errorTips
          })
        }
      })
    })
  },

  // 上传图片
  uploadImg: function (img, fn) {
    var that = this;
    var imgs = that.data.imgs;
    app.getLoginSession(function (session) {
      wx.uploadFile({
        url: app.api,
        filePath: img,
        name: 'file',
        formData: {
          app: 'member',
          act: 'upload_file',
          filePath: img,
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          var data = JSON.parse(res.data);
          var rlt = data.retval;
          if (data.done) {

            imgs.push(rlt.save_path);
            that.setData({
              imgs: imgs
            })
            console.log("sss" + that.data.imgs)
            typeof fn == 'function' && fn();
          } else {
            console.log(data);
            that.tips('图片上传失败')
          }
        },
        fail: function () {
          console.log("失败");
        }
      })
    })
  },

  // 提示框
  tips: function (txt) {
    wx.showModal({
      title: '提示',
      content: txt,
      showCancel: false
    })
  },

  // 选择图片
  chooseImg: function () {
    var that = this;
    var selectImg = that.data.selectImg;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {

        var tempFilePaths = res.tempFilePaths

        if (tempFilePaths.length + selectImg.length > 5) {
          that.tips('最多只能上传5张')
        } else {
          for (var i = 0; i < tempFilePaths.length; i++) {
            selectImg.push(tempFilePaths[i])
            // that.uploadImg(tempFilePaths[i])
          }
          that.setData({
            selectImg: selectImg
          })
          console.log(selectImg);
        }

      },
      fail: function () {
        that.tips('选取图片失败')
      }
    })
  },
  // 删除图片
  deleteImg: function (event) {
    var that = this;
    var selectImg = that.data.selectImg;
    var url = event.currentTarget.dataset.url;
    for (var i in selectImg) {
      if (selectImg[i] == url) {
        selectImg.splice(i, 1);
        break
      }
    }
    that.setData({
      selectImg: selectImg
    });
    console.log(that.data.selectImg);
  },

};

// 注册页面
Page(init);
